package com.cloudptt.wp.ui.more;

import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ScreenManager;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.BaseActivity;
import com.cloudptt.wp.ui.Login;
import com.cloudptt.wp.ui.MainActivity;
import com.mcptt.config.model.McIdInfo;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class SettingActivity extends BaseActivity {

	private final static String TAG = "SettingsActivity";

	private LinearLayout pttLy;
	private ImageView pttIv;
	private TextView pttTv;
	private LinearLayout videoLy;
	private ImageView videoIv;
	private TextView videoTv;
	private LinearLayout dataLy;
	private ImageView dataIv;
	private TextView dataTv;
	private TextView user_num;
	private TextView user_name;
	//	private TextView versionVal;
	private Button exit_bt;
	private Button logout_bt;

	private SettingsAdapter mAdapter;
	private ListView mSettingsListView;

	private ImageView mUserIconImageView;

	private McIdInfo info = null;

	public static int[] item_icon_group = {
			R.drawable.settings_func
			,R.drawable.settings_bluetooth
			,R.drawable.setting_version_upload
			,R.drawable.settings_about_us_icon
//			,R.drawable.settings_version_update
//			,R.drawable.settings_advanced
//			,R.drawable.settings_timer
	};

	public static int[] item_title_group = {
			R.string.settings_func
			,R.string.settings_Server
			,R.string.setting_version_upload
			,R.string.settings_about_us
			//,R.string.settings_version_update_title
//			,R.string.settings_advanced
//			,R.string.settings_timer
	};

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		Window window = getWindow();
		window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		window.setStatusBarColor(getResources().getColor(R.color.gradient_end));
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_activity);

		initView();
		//initData();
		initNotificationLayout();
	}

	@Override
	protected void onResume() {
		super.onResume();
		initData();
	}

	private void initView() {
		TextView tv = (TextView) findViewById(R.id.titleTv);
		tv.setText(R.string.settings_title);
		ImageView infoIv = (ImageView) findViewById(R.id.infoIv);
		infoIv.setVisibility(View.GONE);

		mSettingsListView  = (ListView) findViewById(R.id.memberLv);
		mSettingsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				toAnotherActivity(position);
			}
		});
		mAdapter = new SettingsAdapter();
		mSettingsListView.setAdapter(mAdapter);

		exit_bt = (Button) findViewById(R.id.id_exit);
		exit_bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				doExitAction();
			}
		});

		logout_bt = (Button) findViewById(R.id.id_logout);
		logout_bt.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				doLogoutAction();
			}});



		user_num  = (TextView) findViewById(R.id.user_info_num);
		user_name = (TextView) findViewById(R.id.user_info_name);

		pttLy = (LinearLayout) findViewById(R.id.pttLy);
		pttIv = (ImageView) findViewById(R.id.pttIv);
		pttTv = (TextView) findViewById(R.id.pttTv);

		videoLy = (LinearLayout) findViewById(R.id.videoLy);
		videoIv = (ImageView) findViewById(R.id.videoIv);
		videoTv = (TextView) findViewById(R.id.videoTv);

		dataLy = (LinearLayout) findViewById(R.id.dataLy);
		dataIv = (ImageView) findViewById(R.id.dataIv);
		dataTv = (TextView) findViewById(R.id.dataTv);

		mUserIconImageView = (ImageView) findViewById(R.id.user_icon_img);
//		mUserIconImageView.setOnClickListener(new View.OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				SysApplication app = ((SysApplication) getApplicationContext());
//				Product product = app.getProduct();
//				if(product == null){
//					return;
//				}
//				SysMsgOperator op = (SysMsgOperator)product.getOperator(ProductConst.Func_SysMsg);
//				Set<String> list = new HashSet<String>();
//				list.add("1001");
//				list.add("1002");
//				list.add("1003");
//				list.add("1004");
//				op.sendSelfOrganizingGroupMsg("test",list);
//			}
//		});
		mUserIconImageView.setOnLongClickListener(new View.OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent = new Intent(SettingActivity.this, UserInfoActivity.class);
				startActivity(intent);
				return false;
			}
		});
	}

	@Override
	protected void onDestroy(){
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	private void initData(){
		SysApplication app = ((SysApplication) getApplicationContext());
		Product product = app.getProduct();
		if(product == null){
			return;
		}
		info = product.getSysConfig().getMcInfo();
		Log.d(TAG, "initData info = " + info);
		String mcId = info.getAddrId();
		user_num.setText(mcId);
		user_name.setText(info.getDscp());
		int publishRsult = product.getPublishResult();
		Log.d(TAG, "initData publishRsult = " + publishRsult);
		if((publishRsult & CallAttr.McType_McPtt) == 0)
		{
			Drawable dr = this.getDrawable(R.drawable.rounded_setting_mcx_disable);
			pttLy.setBackground(dr);
			pttIv.setImageResource(R.drawable.setting_ptt_disable);
			pttTv.setTextColor(getResources().getColor(R.color.setting_disable));
		}
		else
		{
			Drawable dr = this.getDrawable(R.drawable.rounded_setting_mcx_enable);
			pttLy.setBackground(dr);
			pttIv.setImageResource(R.drawable.setting_ptt_enable);
			pttTv.setTextColor(getResources().getColor(R.color.setting_enable));
		}
		if((publishRsult & CallAttr.McType_McVideo) == 0)
		{
			Drawable dr = this.getDrawable(R.drawable.rounded_setting_mcx_disable);
			videoLy.setBackground(dr);
			videoIv.setImageResource(R.drawable.setting_video_disable);
			videoTv.setTextColor(getResources().getColor(R.color.setting_disable));
		}
		else
		{
			Drawable dr = this.getDrawable(R.drawable.rounded_setting_mcx_enable);
			videoLy.setBackground(dr);
			videoIv.setImageResource(R.drawable.setting_video_enable);
			videoTv.setTextColor(getResources().getColor(R.color.setting_enable));
		}
		if((publishRsult & CallAttr.McType_McData) == 0)
		{
			Drawable dr = this.getDrawable(R.drawable.rounded_setting_mcx_disable);
			dataLy.setBackground(dr);
			dataIv.setImageResource(R.drawable.setting_data_disable);
			dataTv.setTextColor(getResources().getColor(R.color.setting_disable));
		}
		else
		{
			Drawable dr = this.getDrawable(R.drawable.rounded_setting_mcx_enable);
			dataLy.setBackground(dr);
			dataIv.setImageResource(R.drawable.setting_data_enable);
			dataTv.setTextColor(getResources().getColor(R.color.setting_enable));
		}
//		setLabelVal();
	}

//	private void setLabelVal(){
//		try {
//			PackageManager manager = this.getPackageManager();
//			PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
//			String version = info.versionName + "(" + info.versionCode + ")";
//			versionVal.setText(version);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

	private void toAnotherActivity(int pos){
		Intent intent = new Intent();
		switch (pos) {
			case 0:
				intent = new Intent();
				intent.setClass(this, FuncPreferenceActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
				break;

			case 1:
//			intent = new Intent(this, CustomPositioningActivity.class);
//			startActivity(intent);
				intent = new Intent();
				intent.setClass(this, ServerInfoActivity.class);
				intent.putExtra(ServerInfoActivity.KEY,false);
				startActivity(intent);
				break;

			case 2:
				SysApplication app = ((SysApplication) getApplicationContext());
				Product product = app.getProduct();
				product.beginUpload(SettingActivity.this,null);
				break;

			case 3:
				intent = new Intent(this, VersionInfoActivity.class);
				startActivity(intent);
//				app = ((SysApplication) getApplicationContext());
//				product = app.getProduct();
//				if(product != null)
//				{
//					product.checkCurVersion(SettingActivity.this,true,false,SettingActivity.this);
//				}
				break;

//		case 3:
//			SysApplication app = ((SysApplication) getApplicationContext());
//			Product product = app.getProduct();
//			product.checkCurVersion(this, true, false, SysApplication.APP_KEY);
//			break;
//
//		case 4:
//			intent = new Intent(this, TimerSetting.class);
//            startActivity(intent);
//			break;
//
//		case 5:
//			intent = new Intent(this, TimerSetting.class);
//            startActivity(intent);
//			break;

			default:
				break;
		}
	}

	private void doExitAction(){
		Log.d(TAG, "doExitAction");
		SysApplication app = ((SysApplication) getApplicationContext());
		Product product = app.getProduct();
		product.doExit();

		SharedPreferences sharedPreferences = getSharedPreferences(MainActivity.KEY_HOME, Context.MODE_PRIVATE);
		sharedPreferences.edit().putBoolean(MainActivity.KEY_NAME_ISFIRST, false).commit();

		ScreenManager.getScreenManager().popAllActivity();
		SystemClock.sleep(2000);
		android.os.Process.killProcess(android.os.Process.myPid());
		System.exit(0);

	}

	private void doLogoutAction()
	{
		Log.d(TAG, "doExitAction");
		SysApplication app = ((SysApplication) getApplicationContext());
		Product product = app.getProduct();
		product.doLogout();

		Log.d(TAG, "doExitAction 1");
		ScreenManager.getScreenManager().popAllActivityExceptOne(this,MainActivity.class);
		Log.d(TAG, "doExitAction 2");
		goToLogin();
	}



	class SettingsAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return item_icon_group.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return item_icon_group[position];
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			ViewHolder viewHolder = new ViewHolder();

			if(convertView == null){
				convertView = getLayoutInflater().inflate(R.layout.settings_item_layout, parent, false);
				viewHolder.icon = (ImageView)convertView.findViewById(R.id.settings_item_icon);
				viewHolder.title = (TextView)convertView.findViewById(R.id.settings_item_title);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) convertView.getTag();
			}

			viewHolder.icon.setImageResource(item_icon_group[position]);
			viewHolder.title.setText(item_title_group[position]);

			return convertView;
		}

	}

	class ViewHolder{
		public ImageView icon;
		public TextView title;
	}

}
